package top.yaofengqiao.springcloudsimple.message.biz.domain.sms.service;

import org.springframework.stereotype.Service;
import top.yaofengqiao.springcloudsimple.common.constant.ResponseCode;
import top.yaofengqiao.springcloudsimple.common.util.AppException;
import top.yaofengqiao.springcloudsimple.message.biz.domain.sms.model.valobj.SmsPlatform;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yfq
 * @date 2024/6/22 10:48
 * @description
 */
@Service
public class SmsServiceFactory {
    private final Map<SmsPlatform, ISmsService> smsServiceMap = new ConcurrentHashMap<>();

    public SmsServiceFactory(List<ISmsService> smsServices) {
        for (ISmsService smsService : smsServices) {
            smsServiceMap.put(smsService.platform(), smsService);
        }
    }

    public ISmsService getSmsService(String code) {
        SmsPlatform smsPlatform = SmsPlatform.determineSmsPlatform(code);
        if (smsPlatform == null) throw new AppException(ResponseCode.SMS_PLATFORM_NOT_SUPPORT);

        return smsServiceMap.get(smsPlatform);
    }
}
